﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Repository;
using System.ComponentModel;
using DevExpress.XtraEditors.Controls;
using ProgNetDxComponents.ExtenderProviders.Language;
using ProgNetDxComponents.ExtenderProviders.Settings;
using DevExpress.XtraBars;

namespace ProgNetDxComponents.ExtenderProviders
{
    [Designer(typeof(ProgNetDxComponents.Design.Designers.LanguageDesigner))]
    public class LanguageProvider : ProgNetComponents.ExtenderProviders.BaseExtenderProvider<LanguageSettings>
    {
        public LanguageProvider() 
        {
            ProgNetLanguage.LanguageChanged += new EventHandler(ProgNetLanguage_LanguageChanged);
        }
        public LanguageProvider(IContainer container)
            : base(container)
        {
            ProgNetLanguage.LanguageChanged += new EventHandler(ProgNetLanguage_LanguageChanged);
        }
        
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing)
            {
                ProgNetLanguage.LanguageChanged -= new EventHandler(ProgNetLanguage_LanguageChanged);
            }
        }
        void ProgNetLanguage_LanguageChanged(object sender, EventArgs e)
        {
            RefreshAllObjects();
        }

        protected override void ProcessObject(object sender, LanguageSettings settings)
        {
            if (sender is RadioGroup)
            {
                ProcessRadionGroupItems((sender as RadioGroup).Properties.Items, settings);
            }
            else if (sender is RepositoryItemRadioGroup)
            {
                ProcessRadionGroupItems((sender as RepositoryItemRadioGroup).Items, settings);
            }
            else if(settings.LngString.IsNotNull()) 
            {
                string test = settings.Prefix + ProgNetLanguage.sGetStringFromMgr(settings.Manager, settings.LngString, settings.Parameters) + settings.Surfix;
                if (sender is Control)
                {
                    (sender as Control).Text = test;
                }
                else if(sender is GridColumn)
                {
                    (sender as GridColumn).Caption = test;
                }
                else if (sender is BarItem)
                {
                    (sender as BarItem).Caption = test;
                }
            }
        }
        private void ProcessRadionGroupItems(DevExpress.XtraEditors.Controls.RadioGroupItemCollection items, LanguageSettings settings)
        {
            if (settings.RadioGroupSettings != null &&
                settings.RadioGroupSettings.Count > 0)
            {
                foreach (RadioItemLngInfo s in settings.RadioGroupSettings)
                {
                    foreach (RadioGroupItem item in items)
                    {
                        if (s.ItemValue.IsTheSame(item.Value))
                        {
                            if (s.StringCode.IsNotNull())
                                item.Description = ProgNetLanguage.sGetString(s.StringCode);
                        }
                    }
                }
            }
        }

        public override bool CanExtend(object extendee)
        {
            if (extendee is Label ||
                extendee is LabelControl ||
                extendee is CheckEdit ||
                extendee is GridColumn ||
                extendee is Button ||
                extendee is SimpleButton ||
                extendee is RadioGroup ||
                extendee is RepositoryItemRadioGroup ||
                extendee is BarItem)
                return true;
            return false;

        }
    }
}
